package scantask.server.administrator;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import scantask.server.dataLayer.DataManipulatorInterface;
import scantask.server.dataLayer.SQLManipulator;
import scantask.server.dataLayer.exceptions.AddAreaException;
import scantask.server.dataLayer.exceptions.AddMeteorologyException;
import scantask.server.dataLayer.exceptions.AddMoistureException;
import scantask.server.dataLayer.exceptions.AddPestException;
import scantask.server.dataLayer.exceptions.AddSyntheticObjectException;
import scantask.server.dataLayer.exceptions.AddTemperatureException;
import scantask.server.dataLayer.exceptions.AddUserException;
import scantask.containers.Area;
import scantask.containers.Coordinate;
import scantask.containers.Moisture;
import scantask.containers.Pest;
import scantask.containers.SyntheticObject;
import scantask.containers.Temperature;
import scantask.containers.User;
import scantask.containers.WeekDate;
import scantask.containers.ZoomLevel;
import scantask.containers.exceptions.IllegalAreaException;
import scantask.containers.exceptions.IllegalLocationNameExeption;
import scantask.containers.exceptions.IllegalPasswordException;
import scantask.containers.exceptions.IllegalUserNameException;
import scantask.containers.exceptions.IllegaleRadiusException;
import scantask.containers.exceptions.InvalidCoordinateValueException;
import scantask.containers.exceptions.InvalidMeasurementTypeException;
import scantask.containers.exceptions.InvalidSyntheticObjectTypeException;
import scantask.containers.exceptions.InvalidWeekNumberException;
import scantask.containers.exceptions.InvalidYearException;
import scantask.containers.exceptions.InvalidZoomLevelParametersException;
import scantask.containers.exceptions.UnInitializeObjectException;

public class Administrator {

	public static void main(String[] args) {
		DataManipulatorInterface dataManipulator = new SQLManipulator();
		String order = "";
		while (order.compareTo("q") != 0) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Select one of the following options:");
			System.out.println("(1) Add new users");
			System.out.println("(2) Add synthetic objects");
			System.out.println("(3) Add temperature measurements");
			System.out.println("(4) Add moisture measurements");
			System.out.println("(5) Add pest measurements");
			System.out.println("(6) Add user Areas");
			System.out.println("(7) Add meteorology stations");
			System.out.println("(8) Clear the dataBase (It will delete the database tables, you shall restart the server after that)!!!");
			System.out.println("(9) Press 'q' to quit");
			try {
				order = reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (order.compareTo("1") == 0) {
				addUsers(dataManipulator);
				continue;
			}
			if (order.compareTo("2") == 0) {
				addSyntheticObjects(dataManipulator);
				continue;
			}
			if (order.compareTo("3") == 0) {
				addTemperatures(dataManipulator);
				continue;
			}
			if (order.compareTo("4") == 0) {
				addMoistures(dataManipulator);
				continue;
			}
			if (order.compareTo("5") == 0) {
				addPests(dataManipulator);
				continue;
			}
			if (order.compareTo("6") == 0) {
				addAreas(dataManipulator);
				continue;
			}
			if (order.compareTo("7") == 0) {
				addMeteorologies(dataManipulator);
				continue;
			}
			if (order.compareTo("8") == 0) {
				dataManipulator.clearDataBase();
				break;
			}
		}
	}

	private static void addUsers(DataManipulatorInterface dataManipulator) {
		System.out.println("Enter users xls file path");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String path = "";
		try {
			path = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		File inputWorkbook = new File(path);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);

			String userName = "";
			String password = "";
			String fullName = "";
			double defaultX = 0;
			double defaultY = 0;
			Coordinate coordinate = null;

			Cell cell = null;
			// the first line is title
			for (int i = 1; i < sheet.getRows(); i++) {

				// first cell suppose to be userName
				cell = sheet.getCell(0, i);
				if (cell.getType() == CellType.EMPTY) {
					break;
				}
				userName = cell.getContents();

				// second cell suppose to be password
				cell = sheet.getCell(1, i);
				password = cell.getContents();
				password = encryptPassword(password);

				// third cell suppose to be full name (can be blank)
				cell = sheet.getCell(2, i);
				fullName = cell.getContents();

				// fourth cell suppose to be  DefaultX (can be blank)
				cell = sheet.getCell(3, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					defaultX = (new Double(cell.getContents())).doubleValue();

					// fifth cell suppose to be  DefaultY (can be blank)
					cell = sheet.getCell(4, i);
					if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
						defaultY = (new Double(cell.getContents())).doubleValue();
						try {
							coordinate = new Coordinate(defaultX, defaultY);
						} catch (InvalidCoordinateValueException e) {
							e.printStackTrace();
						}
					}
					else {
						coordinate = null;
					}
				}
				else {
					coordinate = null;
				}

				addUser(dataManipulator, userName, password, fullName, coordinate);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void addSyntheticObjects(DataManipulatorInterface dataManipulator) {
		System.out.println("Enter synthetic objects xls file path");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String path = "";
		try {
			path = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		File inputWorkbook = new File(path);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);

			String userName = "";
			double centerX = 0;
			double centerY = 0;
			int radius = 0;
			String type = "";
			int minView = 0;
			int maxView = 0;

			Coordinate coordinate = null;
			ZoomLevel viewInterval = null;

			Cell cell = null;

			// the first line is title
			for (int i = 1; i < sheet.getRows(); i++) {
				// first cell suppose to be userName
				cell = sheet.getCell(0, i);
				if (cell.getType() == CellType.EMPTY) {
					break;
				}
				userName = cell.getContents();

				// second cell suppose to be CenterX
				cell = sheet.getCell(1, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					centerX = (new Double(cell.getContents())).doubleValue();
				}
				else {
					System.out.println("Synthetic object " + i + " contains illegal centerX and will not added");
					continue;
				}

				// third cell suppose to be CenterY
				cell = sheet.getCell(2, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					centerY = (new Double(cell.getContents())).doubleValue();
				}
				else {
					System.out.println("Synthetic object " + i + " contains illegal centerY and will not be added");
					continue;
				}

				try {
					coordinate = new Coordinate(centerX, centerY);
				} catch (InvalidCoordinateValueException e) {
					System.out.println("Synthetic object " + i + " contains illegal Coordinate and will not be added");
					continue;
				}

				// fourth cell suppose to be radius (can be blank) 
				cell = sheet.getCell(3, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					radius = (new Integer(cell.getContents())).intValue();
				}
				else {
					radius = 1;
				}

				// fifth cell suppose to be type
				cell = sheet.getCell(4, i);
				type = cell.getContents();

				// sixth cell suppose to be  minView (can be blank)
				cell = sheet.getCell(5, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					minView = (new Integer(cell.getContents())).intValue();

					// seventh cell suppose to be  maxView (can be blank)
					cell = sheet.getCell(6, i);
					if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
						maxView = (new Integer(cell.getContents())).intValue();
						try {
							viewInterval = new ZoomLevel(minView, maxView);
						} catch (InvalidZoomLevelParametersException e) {
							System.out.println("Failed initialize ZoomLevel for synthetic object " + i + " the default zoomLevel is null");
							viewInterval = null;
						}
					}
					else {
						viewInterval = null;
					}
				}
				else {
					viewInterval = null;
				}

				addSyntheticObject(dataManipulator, userName, coordinate, viewInterval, type, radius);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void addTemperatures(DataManipulatorInterface dataManipulator) {
		System.out.println("Enter temperatures xls file path");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String path = "";
		try {
			path = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		File inputWorkbook = new File(path);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);

			String userName = "";
			int weekNum = 0;
			int year = 0;
			String meteorology = "";
			double value = 0;
			Cell cell = null;

			// the first line is title
			for (int i = 1; i < sheet.getRows(); i++) {
				// first cell suppose to be userName
				cell = sheet.getCell(0, i);
				if (cell.getType() == CellType.EMPTY) {
					break;
				}
				userName = cell.getContents();

				// second cell suppose to be week num
				cell = sheet.getCell(1, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					weekNum = (new Integer(cell.getContents())).intValue();
				}
				else {
					System.out.println("Temprature object " + i + " contains illegal week # and will not added");
					continue;
				}

				// third cell suppose to be year
				cell = sheet.getCell(2, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					year = (new Integer(cell.getContents())).intValue();
				}
				else {
					System.out.println("Temprature object " + i + " contains illegal year and will not be added");
					continue;
				}
				
				// fourth cell suppose to be meteorology station  
				cell = sheet.getCell(3, i);
				meteorology = cell.getContents();

				// fifth cell suppose to be  value (can be blank)
				cell = sheet.getCell(4, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					value = (new Double(cell.getContents())).doubleValue();	
				}
				else {
					System.out.println("Temprature object " + i + " contains illegal value and will not be added");
					continue;				}

				addTemprature(dataManipulator, userName, weekNum, year, meteorology, value);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static void addMoistures(DataManipulatorInterface dataManipulator) {
		System.out.println("Enter Moistures xls file path");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String path = "";
		try {
			path = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		File inputWorkbook = new File(path);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);

			String userName = "";
			int weekNum = 0;
			int year = 0;
			String meteorology = "";
			double value = 0;
			Cell cell = null;

			// the first line is title
			for (int i = 1; i < sheet.getRows(); i++) {
				// first cell suppose to be userName
				cell = sheet.getCell(0, i);
				if (cell.getType() == CellType.EMPTY) {
					break;
				}
				userName = cell.getContents();

				// second cell suppose to be week num
				cell = sheet.getCell(1, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					weekNum = (new Integer(cell.getContents())).intValue();
				}
				else {
					System.out.println("Moisture object " + i + " contains illegal week # and will not added");
					continue;
				}

				// third cell suppose to be year
				cell = sheet.getCell(2, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					year = (new Integer(cell.getContents())).intValue();
				}
				else {
					System.out.println("Moisture object " + i + " contains illegal year and will not be added");
					continue;
				}
				
				// fourth cell suppose to be meteorology station name  
				cell = sheet.getCell(3, i);
				meteorology = cell.getContents();
				
				// fifth cell suppose to be  value
				cell = sheet.getCell(4, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					value = (new Double(cell.getContents())).doubleValue();	
				}
				else {
					System.out.println("Moisture object " + i + " contains illegal Value and will not be added");
					continue;
				}

				addMoisture(dataManipulator, userName, weekNum, year, meteorology, value);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
/*
	private static void addCrops(DataManipulatorInterface dataManipulator) {
		System.out.println("Enter crops xls file path");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String path = "";
		try {
			path = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		File inputWorkbook = new File(path);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);

			String userName = "";
			String type = "";
			int weekNum = 0;
			int year = 0;
			double x = 0;
			double y = 0;
			double value = 0;
			boolean interpolated = false;
			Cell cell = null;

			// the first line is title
			for (int i = 1; i < sheet.getRows(); i++) {
				// first cell suppose to be userName
				cell = sheet.getCell(0, i);
				userName = cell.getContents();
				
				// 2nd cell suppose to be type
				cell = sheet.getCell(1, i);
				if(cell.getType()!=CellType.EMPTY){
					type = cell.getContents();
				}
				else{
					System.out.println("Crop object " + i + " contains illegal type and will not added");
					continue;
				}
				
				// 3rd cell suppose to be week num
				cell = sheet.getCell(2, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					weekNum = (new Integer(cell.getContents())).intValue();
				}
				else {
					System.out.println("Crop object " + i + " contains illegal week # and will not added");
					continue;
				}

				// 4th cell suppose to be year
				cell = sheet.getCell(3, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					year = (new Integer(cell.getContents())).intValue();
				}
				else {
					System.out.println("Crop object " + i + " contains illegal year and will not be added");
					continue;
				}
				
				// 5th cell suppose to be x  
				cell = sheet.getCell(4, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					x = (new Double(cell.getContents())).doubleValue();
				}
				else {
					System.out.println("Crop object " + i + " contains illegal x coordinate and will not added");
					continue;
				}
				// 6th cell suppose to be y  
				cell = sheet.getCell(5, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					y = (new Double(cell.getContents())).doubleValue();
				}
				else {
					System.out.println("Crop object " + i + " contains illegal y coordinate and will not added");
					continue;
				}

				// 7th cell suppose to be  value (can be blank)
				cell = sheet.getCell(6, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					value = (new Double(cell.getContents())).doubleValue();	
				}
				else {
					value = 0;
					interpolated=true;
				}

				addCrop(dataManipulator, userName, type, weekNum, year, x, y, value, interpolated);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
*/
	private static void addPests(DataManipulatorInterface dataManipulator) {
		System.out.println("Enter Pests xls file path");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String path = "";
		try {
			path = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		File inputWorkbook = new File(path);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);

			String userName = "";
			String type = "";
			int weekNum = 0;
			int year = 0;
			String areaName = "";
			String valueLevel = "";
			double value = 0;
			boolean interpolated = false;
			Cell cell = null;

			// the first line is title
			for (int i = 1; i < sheet.getRows(); i++) {
				userName = "";
				type = "";
				weekNum = 0;
				year = 0;
				areaName = "";
				valueLevel = "";
				value = 0;
				interpolated = false;

				// first cell suppose to be userName
				cell = sheet.getCell(0, i);
				if (cell.getType() == CellType.EMPTY) {
					break;
				}
				userName = cell.getContents();
				
				// 2nd cell suppose to be type
				cell = sheet.getCell(1, i);
				if(cell.getType()!=CellType.EMPTY){
					type = cell.getContents();
				}
				else{
					System.out.println("Pest object " + i + " contains illegal type and will not added");
					continue;
				}
				
				// 3rd cell suppose to be week num
				cell = sheet.getCell(2, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					weekNum = (new Integer(cell.getContents())).intValue();
				}
				else {
					System.out.println("Pest object " + i + " contains illegal week # and will not added");
					continue;
				}

				// 4th cell suppose to be year
				cell = sheet.getCell(3, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					year = (new Integer(cell.getContents())).intValue();
				}
				else {
					System.out.println("Pest object " + i + " contains illegal year and will not be added");
					continue;
				}
				
				// 5th cell suppose to be area name 
				cell = sheet.getCell(4, i);
				areaName = cell.getContents();
				
				// 6th cell suppose to be valueLevel
				cell = sheet.getCell(5, i);
				valueLevel = cell.getContents();
				if (valueLevel.compareTo("") == 0) {
					value = 0;
					interpolated = true;
				}
				else {
					value = scantask.containers.PestsLevelsSigelton.getPestValueOfLevel(valueLevel.toLowerCase());
				}

				addPest(dataManipulator, userName, type, weekNum, year, areaName, value, interpolated);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	private static void addMeteorologies(DataManipulatorInterface dataManipulator) {
		System.out.println("Enter Pests xls file path");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String path = "";
		try {
			path = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		File inputWorkbook = new File(path);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);

			String name = "";
			double x;
			double y;
			Cell cell = null;

			// the first line is title
			for (int i = 1; i < sheet.getRows(); i++) {
				// first cell suppose to be meteorology station name
				cell = sheet.getCell(0, i);
				if (cell.getType() == CellType.EMPTY) {
					break;
				}
				name = cell.getContents();
				
				// 2nd cell suppose to be x
				cell = sheet.getCell(1, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					x = (new Double(cell.getContents())).doubleValue();
				}
				else {
					System.out.println("Meteorology object " + i + " contains illegal x value and will not added");
					continue;
				}
				
				// 3rd cell suppose to be y
				cell = sheet.getCell(2, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					y = (new Double(cell.getContents())).doubleValue();
				}
				else {
					System.out.println("Meteorology object " + i + " contains illegal y value and will not added");
					continue;
				}


				addMeteorology(dataManipulator, name, x, y);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	private static void addAreas(DataManipulatorInterface dataManipulator) {
		System.out.println("Enter crops xls file path");
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String path = "";
		try {
			path = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		File inputWorkbook = new File(path);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);

			String userName = "";
			String type = "";
			String areaName = "";
			String color = "";
			double centerX = 0;
			double centerY = 0;
			Vector<Coordinate> vertexes = new Vector<Coordinate>();

			Cell cell = null;

			// the first line is title
			for (int i = 1; i < sheet.getRows(); i++) {
				// first cell suppose to be userName
				cell = sheet.getCell(0, i);
				if (cell.getType() == CellType.EMPTY) {
					break;
				}
				userName = cell.getContents();
				
				// 2nd cell suppose to be centerX
				cell = sheet.getCell(1, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					centerX = (new Double(cell.getContents())).doubleValue();
				}
				else {
					System.out.println("area object " + i + " contains illegal centerX and will not added");
					continue;
				}
				
				// 3rd cell suppose to be centerY
				cell = sheet.getCell(2, i);
				if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
					centerY = (new Double(cell.getContents())).doubleValue();
				}
				else {
					System.out.println("area object " + i + " contains illegal centerY and will not added");
					continue;
				}

				// 4th cell suppose to be area name
				cell = sheet.getCell(3, i);
				areaName = cell.getContents();
				
				// 5th cell suppose to be crop type
				cell = sheet.getCell(4, i);
				type = cell.getContents();
				
				// 6th cell suppose to be color
				cell = sheet.getCell(5, i);
				color = cell.getContents();
				
				vertexes = new Vector<Coordinate>();
				Coordinate coordinate = null;
				double x;
				double y;
				for (int j = 6; j < sheet.getColumns(); j = j + 2) {
					// cell suppose to be x  
					cell = sheet.getCell(j, i);
					if (cell.getType() == CellType.EMPTY) {
						break;
					}
					if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
						x = (new Double(cell.getContents())).doubleValue();
					}
					else {
						System.out.println("Area object " + i + " contains illegal x coordinate vertex " + j + " and will not added");
						continue;
					}
					// cell suppose to be y  
					cell = sheet.getCell(j+1, i);
					if ((cell.getType() != CellType.EMPTY) && (cell.getType() == CellType.NUMBER)) {
						y = (new Double(cell.getContents())).doubleValue();
					}
					else {
						System.out.println("area object " + i + " contains illegal y coordinate vertex " + j + " and will not added");
						continue;
					}
					try {
						coordinate = new Coordinate(x, y);
					} catch (InvalidCoordinateValueException e) {
						System.out.println("area object " + i + " contains illegal coordinate vertex and will not added");
						continue;
					}
					
					vertexes.add(coordinate);
				}

				addArea(dataManipulator, userName, centerX, centerY, areaName, type, color, vertexes);
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	

	private static void addUser(DataManipulatorInterface dataManipulator, String userName, 
			String password, String fullName, Coordinate coordinate) {

		User user = null;
		try {
			user = new User(userName, password, fullName, coordinate);
		} catch (IllegalUserNameException e) {
			System.out.println("failed add user " + userName);
			e.printStackTrace();
			return;
		} catch (IllegalPasswordException e) {
			System.out.println("failed add user " + userName);
			e.printStackTrace();
			return;
		}

		try {
			dataManipulator.addUser(user);
		} catch (AddUserException e) {
			System.out.println("failed add user " + userName);
			e.printStackTrace();
		}

	}

	private static void addSyntheticObject(DataManipulatorInterface dataManipulator, String userName,
			Coordinate coordinate, ZoomLevel viewInterval, String type, int radius) {

		SyntheticObject synthetic = null;

		try {
			synthetic = new SyntheticObject(coordinate, viewInterval, type, radius);
		} catch (UnInitializeObjectException e) {
			System.out.println("failed add user " + type);
			e.printStackTrace();
			return;
		} catch (IllegaleRadiusException e) {
			System.out.println("failed add user " + type);
			e.printStackTrace();
			return;
		} catch (InvalidSyntheticObjectTypeException e) {
			System.out.println("failed add user " + type);
			e.printStackTrace();
			return;
		}

		try {
			dataManipulator.addSyntheticObject(userName, synthetic);
		} catch (AddSyntheticObjectException e) {
			System.out.println("failed add user " + type);
			e.printStackTrace();
		}

	}

	private static void addTemprature(DataManipulatorInterface dataManipulator,String userName, int weekNum, int year, String meteorology, double value){
		Temperature temp = null;
		try {
			temp = new Temperature(new Coordinate(0,0),null,value,new WeekDate(weekNum,year), meteorology);
		} catch (UnInitializeObjectException e) {
			System.out.println("failed add temprature ");
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			System.out.println("failed add temprature ");
			e.printStackTrace();
		} catch (InvalidWeekNumberException e) {
			System.out.println("failed add temprature ");
			e.printStackTrace();
		} catch (InvalidYearException e) {
			System.out.println("failed add temprature ");
			e.printStackTrace();
		} catch (IllegalLocationNameExeption e) {
			System.out.println("failed add temprature ");
			e.printStackTrace();
		}

		try {
			dataManipulator.addTemperature(userName, temp);
		} catch (AddTemperatureException e) {
			System.out.println("failed add temprature ");
			e.printStackTrace();
		}

	}

	private static void addMoisture(DataManipulatorInterface dataManipulator,String userName, int weekNum, int year, String meteorology, double value){
		Moisture moist = null;
		try {
			moist = new Moisture(new Coordinate(0,0),null,value,new WeekDate(weekNum,year), meteorology);
		} catch (UnInitializeObjectException e) {
			System.out.println("failed add Moisture ");
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			System.out.println("failed add Moisture ");
			e.printStackTrace();
		} catch (InvalidWeekNumberException e) {
			System.out.println("failed add Moisture ");
			e.printStackTrace();
		} catch (InvalidYearException e) {
			System.out.println("failed add Moisture ");
			e.printStackTrace();
		} catch (IllegalLocationNameExeption e) {
			System.out.println("failed add Moisture ");
			e.printStackTrace();
		}

		try {
			dataManipulator.addMoisture(userName, moist);
		} catch (AddMoistureException e) {
			System.out.println("failed add Moisture ");
			e.printStackTrace();
		}

	}

	/*
	private static void addCrop(DataManipulatorInterface dataManipulator,String userName, String type, int weekNum, int year, double x, double y, double value, boolean interpolated){
		Crop crop = null;
		try {
			crop = new Crop(new Coordinate(x,y),null,value, new WeekDate(weekNum,year), interpolated, type);
		} catch (UnInitializeObjectException e) {
			System.out.println("failed add Crop ");
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			System.out.println("failed add Crop ");
			e.printStackTrace();
		} catch (InvalidWeekNumberException e) {
			System.out.println("failed add Crop ");
			e.printStackTrace();
		} catch (InvalidYearException e) {
			System.out.println("failed add Crop ");
			e.printStackTrace();
		} catch (InvalidMeasurementTypeException e) {
			System.out.println("failed add Crop ");
			e.printStackTrace();
		}

		try {
			dataManipulator.addCrop(userName, crop);
		} catch (AddCropException e) {
			System.out.println("failed add Crop ");
			e.printStackTrace();
		}

	}
	*/
	private static void addPest(DataManipulatorInterface dataManipulator,String userName, String type, int weekNum, int year, String areaName, double value, boolean interpolated){
		Pest pest = null;
		try {
			pest = new Pest(new Coordinate(0,0),null,value, new WeekDate(weekNum,year), interpolated, type, areaName);
		} catch (UnInitializeObjectException e) {
			System.out.println("failed add Pest ");
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			System.out.println("failed add Pest ");
			e.printStackTrace();
		} catch (InvalidWeekNumberException e) {
			System.out.println("failed add Pest ");
			e.printStackTrace();
		} catch (InvalidYearException e) {
			System.out.println("failed add Pest ");
			e.printStackTrace();
		} catch (InvalidMeasurementTypeException e) {
			System.out.println("failed add Pest ");
			e.printStackTrace();
		} catch (IllegalLocationNameExeption e) {
			System.out.println("failed add Pest ");
			e.printStackTrace();
		}

		try {
			dataManipulator.addPest(userName, pest);
		} catch (AddPestException e) {
			System.out.println("failed add Crop ");
			e.printStackTrace();
		}

	}
	
	private static void addMeteorology(DataManipulatorInterface dataManipulator, String name, double x, double y) {
		try {
			dataManipulator.addMeteorologyStation(name, x, y);
		} catch (AddMeteorologyException e) {
			System.out.println("failed add Meteorology station");
			e.printStackTrace();
		}
	}
	
	
	private static void addArea(DataManipulatorInterface dataManipulator, String userName, double centerX, double centerY,
			String areaName, String cropType, String color, Vector<Coordinate> vertexes) {
		Area area = null;
		try {
			area = new Area(new Coordinate(centerX, centerY), vertexes, areaName, cropType, color);
			dataManipulator.addArea(userName, area);
		} catch (IllegalAreaException e) {
			System.out.println("failed add area");
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			System.out.println("failed add area");
			e.printStackTrace();
		} catch (AddAreaException e) {
			System.out.println("failed add area");
			e.printStackTrace();
		}
	}
	
	
	public static String encryptPassword(String password) {
		// encrypting the password
		String storepass = password;
		try
		{
			java.security.Security.addProvider(new sun.security.provider.Sun());
			java.security.MessageDigest lMessageDigest = java.security.MessageDigest.getInstance("SHA", "SUN");
			byte[] result = lMessageDigest.digest(password.getBytes());
			storepass = new sun.misc.BASE64Encoder().encode(result );
		}
		catch(java.security.NoSuchProviderException nspe)
		{
			// ...
		}
		catch(java.security.NoSuchAlgorithmException nsae)
		{
			// ...
		}

		return storepass;

	}

}
